package com.xplay.xpocker.room.mahjong.action;

import com.xplay.xpocker.constant.MessageNoticeEnum;
import com.xplay.xpocker.dvo.GameRuMahjongUserState;
import com.xplay.xpocker.entity.GameRuMahjongLogs;
import com.xplay.xpocker.entity.GameRuMahjongOpenDeal;
import com.xplay.xpocker.room.RoomInfoTL;
import com.xplay.xpocker.room.mahjong.AbstractIsolationEventStrategy;
import com.xplay.xpocker.room.mahjong.AbstractMahjongMessageStrategy;
import com.xplay.xpocker.room.mahjong.constant.MKey;
import com.xplay.xpocker.room.mahjong.dto.MahjongActionEnum;
import com.xplay.xpocker.room.mahjong.dto.MahjongMessage;
import com.xplay.xpocker.room.mahjong.dto.MahjongRoomInfo;
import com.xplay.xpocker.room.mahjong.dto.MahjongRoomRule;
import com.xplay.xpocker.room.message.MessageContent;
import com.xplay.xpocker.room.message.MessageContentFactory;
import com.xplay.xpocker.room.message.MessageToClient;
import com.xplay.xpocker.room.message.MessageType;
import com.xplay.xpocker.service.gaming.GameRuMahjongOpenDealService;
import com.xplay.xpocker.dvo.AbstractRoom;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BumpAction extends AbstractMahjongMessageStrategy<AbstractRoom<MahjongRoomRule, MahjongRoomInfo,GameRuMahjongLogs>, MahjongMessage> {
    @Autowired
    private GameRuMahjongOpenDealService gameRuUserOpenDealService;

    @Override
    protected String strategyName() {
        return MahjongActionEnum.BUMP.getAction();
    }

    // TODO 碰牌需要判断有没有人要胡牌 碰牌优先低于胡牌
    @Override
    public boolean doMessage(AbstractRoom<MahjongRoomRule, MahjongRoomInfo, GameRuMahjongLogs> roomInfo, MahjongMessage messageInfo) {
        MahjongRoomInfo gameInfo = roomInfo.getGameInfo();
        Integer cardCode = gameInfo.getExportCard();
        GameRuMahjongUserState currentUser = gameInfo.getCurrentUser();
        // 检测房间中其他玩家是不是有胡的
        if (checkWaitCompletedExistsAction(MahjongActionEnum.HU)) {
            currentUser.saveNextAction(MahjongActionEnum.BUMP,messageInfo);
            notifyUserActionUpdate(currentUser, 0);
            return false;
        }

        GameRuMahjongUserState exportUser = getUserByUidOrSeq(gameInfo.getExportUserId(), null);
        exportUser.setExportCards(exportUser.deleteLastCard(exportUser.getExportCards()));
        // 待处理为杠牌
        currentUser.setWaitCompleted(MahjongActionEnum.EXPORT_CARD.getAction());
        currentUser.removeCard(cardCode,cardCode);
        GameRuMahjongOpenDeal userOpenDeal = currentUser.getOpenDealByCard(cardCode);
        userOpenDeal.setTargetUserSeq(gameInfo.getExportUserSeq());
        userOpenDeal.setTargetUserId(gameInfo.getExportUserId());

        userOpenDeal.setSourceUserId(currentUser.getUserId());
        userOpenDeal.setSourceUserSeq(currentUser.getSeq());
        userOpenDeal.setRoomId(roomInfo.getGameInfo().getRoomId());
        userOpenDeal.setCard(cardCode);
        userOpenDeal.setType(1);
        currentUser.addOpenDeal(userOpenDeal);
        currentUser.setCompletedData(null);
        log.debug("房间号:{},用户:{},执行碰牌：{}",roomInfo.getCode(),currentUser.getUsername(),cardCode);
        //正常碰牌过后事件检测
        AbstractIsolationEventStrategy.getSettlementStrategy(roomInfo.getType())
                .bumpCardActionCheck(roomInfo, currentUser, exportUser, cardCode);


        List<MessageContent<MessageToClient>> messageList = MessageContentFactory.
                init(roomInfo.getCode(), MessageType.ACTION.getType(), currentUser.getUserId(), MessageNoticeEnum.ALL)
                .multi(MKey.exeActionUid, currentUser.getUserId(),
                        MKey.sourceBySeq, userOpenDeal.getTargetUserSeq(),
                        MKey.cardCode, userOpenDeal.getCard(),
                        MKey.cardType, userOpenDeal.getType(),
                        MKey.type, MahjongActionEnum.BUMP.getAction())
                .send().getMessageList();
        RoomInfoTL.addMessageToClient(messageList);
        return true;
    }

}
